import { Component, OnInit } from '@angular/core';
import { HeroService } from '../../../../services/hero.service';
import { NzMessageService,NzModalService } from 'ng-zorro-antd';
import { Router } from '@angular/router';

@Component({
  selector: 'app-addnotdealfault',
  templateUrl: './addnotdealfault.component.html',
  styleUrls: ['./addnotdealfault.component.css']
})
export class AddnotdealfaultComponent implements OnInit {

  isVisible = false;
  btn_loading=false;
  isSignVisible = false;
  removeVisible = false;
  loading = true;
  public brandName:string;
  public brandlist:Array<any>=[];
  public datalist:Array<any>=[];
  public exclude:Array<any>=[];
  public brandinfo:string;
  public childValue:Array<any>=[];
  public disableTasklist:Array<any>=[];
  public disableTaskName:string;
  public PageIndex:number=1;
  public PageSize:number=6;
  public PageTotal:number;
  allChecked = false;
  indeterminate = false;
  constructor(private heroService: HeroService,
              private router: Router,
              private message: NzMessageService,
              private modalService: NzModalService) { }

  ngOnInit() {
    //获取品牌
    let url=host+"/main/util/getBrand";
    this.heroService.get(url).then(result=>{
        if(result.code==0){
          // this.brandName=String(result.data[0].id);
          this.brandName='-1';
          this.brandlist=result.data;
          this.getDatalist();
        }else{
          this.modalService.error({
            nzTitle: '提示',
            nzContent: result.msg
          });
        }
    })
  }

  //获取不处理故障列表数据
  getDatalist(){
    let url=host+"/main/merchantExcludeTasks/add?brandId="+this.brandName+"&page="+this.PageIndex+"&pageSize="+this.PageSize;
    this.heroService.get(url).then(result=>{
      if(result.code==0){
        this.datalist=result.data.pageBean.result;
        this.exclude=result.data.exclude;
        this.datalist.forEach(data => {
          data.checked=false;
          // if(this.exclude.includes(data.id)){
          //   data.disabled=true;
          // }
        });
        this.brandinfo=result.data.brand;
        this.PageIndex=result.data.pageBean.curPage;  
        this.PageTotal=result.data.pageBean.totalRow;
        this.loading=false;
      }else{
        this.modalService.error({
          nzTitle: '提示',
          nzContent: result.msg
        });
      }
    })
  }

  //分页
  PageIndexChange(event){
    this.PageIndex=event;
    this.getDatalist();
  }

  //全选
  refreshStatus(): void {
    // const allChecked = this.datalist.filter(value => !value.disabled).every(value => value.checked === true);
    // const allUnChecked = this.datalist.filter(value => !value.disabled).every(value => !value.checked);
    const allChecked = this.datalist.every(value => value.checked === true);
    const allUnChecked = this.datalist.every(value => !value.checked);
    this.allChecked = allChecked;
    this.indeterminate = (!allChecked) && (!allUnChecked);
  }

  checkAll(value: boolean): void {
    // this.datalist.filter(value => !value.disabled).forEach(data => {
    this.datalist.filter(value => !value.disabled).forEach(data => {
        data.checked = value;
    });
    this.refreshStatus();
  }
  
  //确认添加
  addNotdealFault(){
    let checklist=[];
    this.datalist.forEach(data => {
      if (data.checked) {
          checklist.push(data.id);
      }
    });
    if(checklist.length > 0){
      this.btn_loading=true;
      let url=host+"/main/merchantExcludeTasks/save";
      let model="tasksTypeIds="+checklist;
      this.heroService.post(url,model).then(result=>{
        this.btn_loading=false;
        if(result.code==0){
          this.message.success("添加成功");
          this.router.navigate(['/main/notdealfault']);
        }else{
          this.modalService.error({
            nzTitle: '提示',
            nzContent: result.msg
          });
        }
      })
    }else{
      this.message.warning('请至少选择一个故障信息！');
    }
    console.log(checklist);
  }
}

